package org.jxkj.data.edos;

import com.ybsoftware.ApiEntry;
import com.ybsoftware.DNAVal;
import org.apache.commons.collections4.CollectionUtils;
import org.jxkj.bean.Point;
import org.jxkj.bean.PointData;
import org.jxkj.client.NodeJsClient;
import org.jxkj.data.BaseDaoProvider;
import org.jxkj.data.IDaoProvider;
import org.jxkj.dto.PointDataDTO;
import org.jxkj.utils.ParseUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * User: jiangfy
 * Date: 2021/7/10
 */
public class EdosApiImpl extends BaseDaoProvider implements IDaoProvider {
    private ApiEntry edosapi;
    SimpleDateFormat ss = new SimpleDateFormat("yyyy/MM/dd");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    public EdosApiImpl(ApiEntry edosapi) {
        this.edosapi = edosapi;
    }

    @Override
    public List<PointData> getByRegion(String pointName, String startTime, String endTime) throws Exception {
        // 判断是否是原始测点
        String calcFlag = Point.getCalcFlag(pointName);
        if ("NORMAL".equals(calcFlag)) {
            int ts1 = (int) (sdf.parse(startTime).getTime() / 1000);
            int ts2 = (int) (sdf.parse(endTime).getTime() / 1000) - 1;
            DNAVal[] data = edosapi.getHistRaw(pointName, ts1, ts2);
            return Arrays.asList(data).stream().map(val -> convertPointData(pointName, val)).collect(Collectors.toList());
        } else {
            // 从nodejs服务查
            int ts1 = (int) (sdf.parse(startTime).getTime() / 1000);
            int ts2 = (int) (sdf.parse(endTime).getTime() / 1000) - 1;
            List<PointDataDTO> list = NodeJsClient.getPointValSnap(pointName, ts1, ts2);
            return toPointDataList(pointName, list);
        }
    }



    @Override
    public List<PointData> getByRegionByDay(String pointName, String startTime, String endTime) throws Exception {
        // 判断是否是原始测点
        // todo:
        String calcFlag = Point.getCalcFlag(pointName);
        if ("NORMAL".equals(calcFlag)) {
            int ts1 = (int) (sdf.parse(startTime).getTime() / 1000);
            int ts2 = (int) (sdf.parse(endTime).getTime() / 1000);
            DNAVal[] data = edosapi.getHistRaw(pointName, ts1, ts2);
            return Arrays.asList(data).stream().map(val -> convertPointData(pointName, val)).collect(Collectors.toList());
        } else {
            // 从nodejs服务查
            int ts1 = (int) (sdf.parse(startTime).getTime() / 1000);
            int ts2 = (int) (sdf.parse(endTime).getTime() / 1000);
            List<PointDataDTO> list = NodeJsClient.getPointValSnap(pointName, ts1, ts2);
            return toPointDataList(pointName, list);
        }
    }

    private PointData convertPointData(String pointName, DNAVal v) {
        PointData p = new PointData();
        p.setName(pointName);
        p.setTime(ParseUtils.getDate(v.Time));
        p.setValue(String.valueOf(v.DValue));
        return p;
    }

    private List<PointData> toPointDataList(String pointCode, List<PointDataDTO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>(0);
        }
        List<PointData> result = new ArrayList<>();
        for (PointDataDTO pointDataDTO : list) {
            PointData pointData = new PointData();
            pointData.setName(pointCode);
            pointData.setTime(ParseUtils.getDate(pointDataDTO.getTime()));
            pointData.setValue(String.valueOf(pointDataDTO.getValue()));
            pointData.setTimestamp(pointDataDTO.getTime());
            result.add(pointData);
        }
        return result;
    }

}
